<!-- 协助管理 -->
<template>
  <div class="assist-manage">
    <el-container v-show="!showFinish">
      <el-header height="40px" style="text-align: center">
        <!-- Header content -->
        <div class="banner">
          <img src="../img/xuexi.png" />
          <span class="title">协助管理</span>
        </div>
      </el-header>
      <el-main height="auto">
        <!-- Main content -->
        <el-table :data="assistList" style="width: 100%">
          <el-table-column property="id" label="订单号"></el-table-column>
          <el-table-column property="title" label="求助标题"></el-table-column>
          <el-table-column
            property="publisherName"
            label="求助人"
          ></el-table-column>
          <el-table-column label="报酬">
            <template slot-scope="scope">
              {{ scope.row.reward + "￥" }}
            </template>
          </el-table-column>
          <el-table-column
            property="createTime"
            label="创建时间"
            min-width="160px"
          ></el-table-column>
          <el-table-column property="status" label="状态">
            <template slot-scope="scope">
              {{ getStatus(scope.row) }}
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-dropdown
                szie="mini"
                placement="bottom-end"
                style="margin-right: 10px"
                trigger="click"
              >
                <span class="el-dropdown-link">
                  <el-button type="text"
                    >选择操作<i class="el-icon-arrow-down el-icon--right"></i
                  ></el-button>
                </span>
                <el-dropdown-menu slot="dropdown" style="width: 96px">
                  <el-dropdown-item
                    @click.native="clickDetail(scope.row.id, scope.$index)"
                  >
                    查看详情
                  </el-dropdown-item>

                  <el-dropdown-item
                    :disabled="
                      scope.row.status == '1' ||
                      scope.row.status == '2' ||
                      scope.row.status == '3'
                    "
                    @click.native="clickFinish(scope.row.id, scope.$index)"
                    >完成任务</el-dropdown-item
                  >
                  <el-dropdown-item
                    @click.native="deleteAssist(scope.row.id, scope.$index)"
                    >取消协助</el-dropdown-item
                  >
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>
      </el-main>

      <el-footer height="35px" style="text-align: center">
        <!-- Footer content -->
        <el-pagination
          :page-size="params.pageSize"
          :current-page="params.pageNum"
          @current-change="changePageNum"
          layout="prev, pager, next"
          :total="total"
        >
        </el-pagination>
      </el-footer>
    </el-container>
    <assist-verify-dialog
      :title="isFinish ? '完成协助任务' : '协助详情'"
      :assistDetail="assistDetail"
      :dialogVisible="dialogVisible"
      :handleClose="handleClose"
      :isFinish="isFinish"
      :isDetail="isDetail"
      :isVerify="false"
      @infoChange="assistInfoChange"
      @confirm="isFinish ? finishAssist() : (dialogVisible = false)"
    ></assist-verify-dialog>
  </div>
</template>

<script>
import {
  deleteAssistById,
  getAssistList,
  getAssistDetail,
  finishAssist,
} from "@/api/assist";
import BreadcrumbVue from "@/components/Breadcrumb.vue";
import AssistVerifyDialog from "@/components/AssistVerifyDialog.vue";
export default {
  name: "assistManagement",
  data() {
    return {
      finishBtnDisabled: false,
      isFinish: null,
      isDetail: null,
      assistInfo: "", //协助完成说明
      assistDetail: {},
      dialogVisible: false,
      selectedIndex: null,
      showFinish: false, //控制完成按钮能不能点
      params: {
        pageSize: 10,
        pageNum: 1,
      },
      total: 100,
      assistList: [],
    };
  },
  components: {
    BreadcrumbVue,
    AssistVerifyDialog,
  },
  methods: {
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(() => {
          console.log("asdfasdfasdf");
          this.dialogVisible = false;
          done();
        })
        .catch(() => {});
    },
    changePageNum(currentPage) {
      console.log(currentPage);
      //这里不能直接设置params里的属性因为默认的代理只会代理一层
      this.$set(this.params, "pageNum", currentPage);
    },
    assistInfoChange(info) {
      console.log("监听到协助信息改变：", info);
      this.assistInfo = info;
    },
    getStatus(assist) {
      switch (assist.status) {
        case "0":
          return "待完成";
        case "1":
          return "待审核";
        case "2":
          return "审核通过";
        case "3":
          return "审核不通过";
        default:
          return "待完成";
      }
    },
    test() {},
    clickFinish(assistId, index) {
      this.selectedIndex = index;
      this.isFinish = true;
      this.isDetail = false;
      this.dialogVisible = true;
      getAssistDetail(assistId).then((res) => {
        this.assistDetail = res;
        console.log("协助详情：", res);
      });
    },
    clickDetail(assistId, index) {
      this.selectedIndex = index;
      this.isFinish = false;
      this.isDetail = true;
      this.dialogVisible = true;
      getAssistDetail(assistId).then((res) => {
        this.assistDetail = res;
        console.log("协助详情：", res);
      });
    },
    finishAssist() {
      console.log("asdfasdf");
      this.dialogVisible = false;
      finishAssist({
        id: this.assistDetail.id,
        comment: this.assistInfo,
      }).then((res) => {
        this.$message({
          type: "success",
          message: "成功完成任务",
        });
        this.getPageData();
      });
    },

    getPageData() {
      getAssistList(this.params).then((res) => {
        console.log(res.rows);
        this.assistList = res.rows;
        this.total = res.total;
      });
    },
    deleteAssist(assistId, index) {
      deleteAssistById(assistId).then((res) => {
        this.getPageData();
        this.$message({
          message: "取消成功",
          type: "success",
        });
      });
    },
  },
  watch: {
    params: {
      deep: true,
      handler: function (newValue, oldValue) {
        this.getPageData();
      },
    },
  },
  created() {
    this.getPageData();
  },
};
</script>

<style  scoped>
* {
  margin: 0px;
  padding: 0px;
}
.assist-manage {
  width: 100%;
  height: 100%;
  display: flex;
}
img {
  width: 35px;
  height: 35px;
  vertical-align: middle;
}
.title {
  font-size: 25px;
  color: rgb(163, 220, 243);
  display: inline-block;
  vertical-align: middle;
}
</style>